package com.sansecy.customview.drawArc;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class FullPieChart extends View {

    private int mW;
    private int mH;
    private RectF rectF;
    private Paint mPaint;
    private List<Float> mPercents;
    private float mStartAngel = 0;
    private List<PieData> mPieData;
    private boolean doAnimation = false;
    private float mCoverStartAngel;
    private float mCoverSweepAngle;
    DecimalFormat df = new DecimalFormat("0.00%");
    /**
     * 动画起始角
     */
    private int mAnimationStartAngel = 0;

    public FullPieChart(Context context) {
        super(context);
        init();
    }

    public FullPieChart(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        rectF = new RectF();
        mPaint = new Paint();
        mPercents = new ArrayList<>();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
    }


    public void setPieData(List<PieData> pieData) {
        mPieData = pieData;

        float total = 0;
        for (int i = 0; i < pieData.size(); i++) {
            PieData data = pieData.get(i);
            total += data.value;
        }
        mPercents.clear();
        for (int i = 0; i < pieData.size(); i++) {
            PieData data = pieData.get(i);
            mPercents.add(360f * data.value / total);
        }
        if (doAnimation) {
            //正向方式绘制遮盖
            ValueAnimator animator = ValueAnimator.ofFloat(mAnimationStartAngel, 360f + mAnimationStartAngel);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    mCoverStartAngel = (float) animation.getAnimatedValue();
                    mCoverSweepAngle = 360f + mAnimationStartAngel - mCoverStartAngel;
                    invalidate();
                }
            });
            animator.setDuration(3000);
            animator.start();

        } else {
            invalidate();
        }
    }

    public void setStartAngel(int startAngel) {
        mStartAngel = startAngel;
    }

    /**
     * 注意 ：角度0是正向x轴
     * 要从更早的方向开始，请传负值
     *
     * @param animationStartAngel 角度
     */
    public void setAnimationStartAngel(int animationStartAngel) {
//        if (animationStartAngel < 0 || animationStartAngel > 360) {
//            throw new IllegalArgumentException("animationStartAngel must in 0 < angel < 360 ");
//        }
        mAnimationStartAngel = animationStartAngel;
    }

    /**
     * 设置是否显示动画
     * 注意，调用此方法请务必放在setPieData()之前，否则动画将不生效
     *
     * @param doAnimation 是否启用动画
     */
    public void setDoAnimation(boolean doAnimation) {
        this.doAnimation = doAnimation;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mPieData == null || mPieData.size() == 0 || mPercents == null) {
            return;
        }
        float startAngel = mStartAngel;
        for (int i = 0; i < mPieData.size(); i++) {
            PieData data = mPieData.get(i);
            mPaint.setColor(data.color);
            float sweepAngle = mPercents.get(i);
            canvas.drawArc(rectF, startAngel, sweepAngle, true, mPaint);
            startAngel += sweepAngle;
        }
        if (doAnimation) {
            mPaint.setColor(Color.WHITE);
            //正向方式绘制遮盖
            canvas.drawArc(rectF, mCoverStartAngel, mCoverSweepAngle, true, mPaint);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mW = w;
        mH = h;
        rectF.left = 0;
        rectF.top = 0;
        rectF.right = w;
        rectF.bottom = h;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = measureDimension(200, widthMeasureSpec);
        int height = measureDimension(200, heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    public int measureDimension(int defaultSize, int measureSpec) {
        int result;

        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = defaultSize;   //UNSPECIFIED
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    public static class PieData {
        private float value;
        private String name;
        private int color;

        public PieData(float value, String name, int color) {
            this.value = value;
            this.name = name;
            this.color = color;
        }

        public int getColor() {
            return color;
        }

        public void setColor(int color) {
            this.color = color;
        }

        public float getValue() {
            return value;
        }

        public void setValue(float value) {
            this.value = value;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

}
